﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace E1_eMart
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected List<Comment> comments = new List<Comment>();
        protected List<TaiKhoan> listTaiKhoanComment = new List<TaiKhoan>();
        protected List<SanPham> spham = new List<SanPham>();
        protected List<string> moTaSP = new List<string>();
        protected List<ThongTinTaiKhoan> nguoiDang = new List<ThongTinTaiKhoan>();
        public TaiKhoan tk = new TaiKhoan();
        protected int loaiTaiKhoan = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            DataClasses1DataContext edata = new DataClasses1DataContext();
            if (Session["username"] != null)
            {
                tk = (TaiKhoan)Session["username"];
                loaiTaiKhoan = (int)tk.MaLoaiTaiKhoan;
            }
            ////Add comment: need login
            //if (Session["username"] != null)
            //{
            //    if (!String.IsNullOrEmpty(Request["comment"]))
            //    {
            //        //Save comment                   
            //        Comment c = new Comment();                   
            //        c.NoiDungComment = Request["comment"];
            //        c.NgayComment = DateTime.Now;                 
            //        tk = (TaiKhoan)Session["username"];                    
            //        c.MaTaiKhoan = tk.MaTaiKhoan;
            //        int maSanPham = int.Parse(Request["id"]);
            //        c.MaSanPham = maSanPham;
            //        edata.Comments.InsertOnSubmit(c);
            //        edata.SubmitChanges();                    
            //    }
            //}
            
            //Show all comments by id of SanPham           
            int maSP = int.Parse(Request["id"]);
            var itemQuery = from cm in edata.Comments
                            where cm.MaSanPham == maSP
                            select cm;
            comments = itemQuery.ToList();

            //Get listTaiKhoanComment
            for (int i = 0; i < comments.Count; i++)
            {
                TaiKhoan taiKhoan = new TaiKhoan();
                var itemQuery1 = from tk in edata.TaiKhoans
                                 where tk.MaTaiKhoan == comments[i].MaTaiKhoan
                                 select tk;
                taiKhoan = itemQuery1.ToList()[0];
                listTaiKhoanComment.Add(taiKhoan);
            }
            
            //Get SanPham to show
            var itemQuery2 = from sp in edata.SanPhams
                             where sp.MaSanPham == maSP
                             select sp;
            spham = itemQuery2.ToList();

            //Get MoTaSanPham to list
            moTaSP = spham[0].MoTaSanPham.Split('.').ToList();
            
            //Get the seller's information
            var itemQuery3 = from tt in edata.ThongTinTaiKhoans
                             where tt.MaTaiKhoan == spham[0].MaTaiKhoan
                             select tt;
            nguoiDang = itemQuery3.ToList();
        }
    }
}